package com.cskaoyan.salary;

import com.cskaoyan.salary.bean.Employee;
import com.cskaoyan.salary.utils.DruidUtils;
import com.cskaoyan.salary.utils.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Savepoint;
import java.util.List;

@WebServlet("/transfer")
public class TransServlet extends HttpServlet {


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //
        Employee boss = null;
        response.setContentType("text/html;charset=utf-8");
        //获得待发薪水的员工列表
        List<Employee> transferList = (List<Employee>) request.getAttribute("transferList");
        //获得全部人员信息
        List<Employee> employeeList = (List<Employee>) request.getSession().getAttribute("employeeList");
        //获得boss的信息
        for (Employee employee:employeeList){
            if (employee.getName().equals("boss")){
                boss = employee;
            }
        }
        //开始转账

//        try {
//            connection = JDBCUtils.getConnection();
//            connection.setAutoCommit(false);
//            //设置非自动提交，这样就在一个事务里面了
//            preparedStatement = connection.prepareStatement("update t_account set money = money + ? where id = ?");
//            preparedStatement2 = connection.prepareStatement("insert into t_record values (null,?,?,?,now())");
//            for (int i = 0; i < transferList.size(); i++) {
//                //获得员工工资
//                preparedStatement.setDouble(1,transferList.get(i).getSalary());
//                preparedStatement.setInt(2,transferList.get(i).getId());
//                preparedStatement.executeUpdate();
//                if(i == 3){
//                    int a = 1 / 0;
//                }
//                savepoint = connection.setSavepoint();
//                //老板减少资金
//                preparedStatement.setDouble(1,-transferList.get(i).getSalary());
//                preparedStatement.setInt(2,boss.getId());
//                //向记录表中写入数据
//                preparedStatement2.setInt(1, boss.getId());
//                preparedStatement2.setInt(2, transferList.get(i).getId());
//                preparedStatement2.setDouble(3, transferList.get(i).getSalary());
//                preparedStatement2.executeUpdate();
//            }

        Connection connection = null;

        try{
            DataSource dataSource = DruidUtils.getDataSource();
            connection = dataSource.getConnection();
            QueryRunner queryRunner = new QueryRunner(dataSource);
            connection.setAutoCommit(false);
            for (int i = 0; i < transferList.size(); i++) {
                queryRunner.update(connection,"update t_account set money = money + ? where id = ?",transferList.get(i).getSalary(),transferList.get(i).getId());
                queryRunner.update(connection,"update t_account set money = money + ? where id = ?",-transferList.get(i).getSalary(),boss.getId());

                queryRunner.update(connection,"insert into t_record values (null,?,?,?,now())",boss.getId(),transferList.get(i).getId(),transferList.get(i).getSalary());
            }
            //提交数据
            connection.commit();
            response.getWriter().println("转账成功");
            //跳转到记录表
            response.setHeader("refresh","2;url=" + request.getContextPath() + "/record");
        } catch (Exception e) {
            e.printStackTrace();
            if(connection != null){
                try {
                    connection.rollback();
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
